import {
	getItem,
	setItem
} from '@/utils/storage'

import {
	LANG,
	TAGS_VIEW
} from '@/constant'

export default {
	namespaced: true,

	state: () => ({
		sidebarOpend: true,
		language: getItem(LANG) || 'zh',
		tagsViewList: getItem(TAGS_VIEW) || []
	}),

	mutations: {
		triggerSidebarOpened(state) {
			state.sidebarOpened = !state.sidebarOpened
		},

		setLanguage(state, lang) {
			state.language = lang
			setItem(LANG, lang)
		},

		addTagsViewList(state, tag) {
			const isFind = state.tagsViewList.find(item => {
				return item.path === tag.path
			})

			if (!isFind) {
				state.tagsViewList.push(tag)
				setItem(TAGS_VIEW, state.tagsViewList)
			}
		},

		changeTagsView(state, {
			index,
			tag
		}) {
			state.tagsViewList[index] = tag
			setItem(TAGS_VIEW, state.tagsViewList)
		},

		removeTagsView(state, payload) {
			if (payload.type === 'index') {
				state.tagsViewList.splice(payload.index, 1)
				return
			} else if (payload.type === 'other') {
				state.tagsViewList.splice(
					payload.index + 1,
					state.tagsViewList.length - payload.index + 1
				)
				state.tagsViewList.splice(0, payload.index)
			} else if (payload.type === 'right') {
				state.tagsViewList.splice(
					payload.index + 1,
					state.tagsViewList.length - payload.index + 1
				)
			}
			setItem(TAGS_VIEW, state.tagsViewList)
		}
	},
}
